$(function(){
	initBlockUI();
	initAddNewPosition();
	initSetPositionPower();
	initCompanySelection();
	initFormSubmit();
});

// 初始化“添加新部门”按钮
function initAddNewPosition()
{
	$("#add_new_position").click(function(){
		$("#add_block").modal({
			onShow: function(){
				$("#opration_panel input").css("visibility", "hidden");
				getCompanyListViaJs("#add_block_belongto", -1);
				$("#add_block_belongto").change(function(){
					getSectionListViaJs("#add_block_belongto_section", $(this).val(), -1);
				});
			},
			onClose: function(){
				$("#opration_panel input").css("visibility", "visible");
				$.modal.close();
			}
		});
		return false;
	});
}

// 初始化“设置部门权限”按钮
function initSetPositionPower()
{
	$("#hopto_position_power").click(function(){
		window.open("position_power.php");
		return false;
	});
}

// 初始化“部门”按钮列表
function initCompanySelection()
{
	getCompanyListViaJs("#company_choise", -1);
	$("#company_choise").change(function(){
		var id = $(this).val();
		var pos_ids = {};
		getSectionButtonsViaJs("#section_list", id);
		$("#section_list input").each(function(i) {
			var sid			= $(this).attr("sid");
			var power		= $(this).attr("power");
			var masterid		= $(this).attr("masterid");
			var inputname	= $(this).val();
			pos_ids[sid]	= { power: power, masterid: masterid, inputname: inputname };
		});
		$("#section_list").html("");
		$.each(pos_ids, function(i, n) {
			$("#section_list").append($("<a></a>")
				.attr("href", "#")
				.attr("sid", i)
				.attr("power", n["power"])
				.attr("masterid", n["masterid"])
				.html(n["inputname"])
				.addClass("buttons")
				.addClass("large_button")
				.click(function() {
					initDataTable($(this).attr("sid"));
					return false;
				})
			);
		});
	});
}

// 初始化数据表格
function initDataTable(id)
{
	var url = "set_position.php";
	var pars = {
		page_status: 1,
		id: id
	};
	$.post(url, pars, function(xmlobj){
		$("#tbody2").html("");
		$(xmlobj).find("item").each(function(i){
			var enname = $(this).find("enname").text();
			var zhname = $(this).find("zhname").text();
			$("<tr></tr>")
				.append($("<th></th>").html(i + 1))
				.append($("<td></td>").html(enname))
				.append($("<td></td>").html(zhname))
			.appendTo("#tbody2");
		});
		tablePretty("#tbody2");
	});
}

// 检测表单内容是否符合条件
function checkForm()
{
	var enname = $("#add_block_enname").val();
	var zhname = $("#add_block_zhname").val();
	var belongto = $("#add_block_belongto_section").html() === "" || $("#add_block_belongto_section").val() == -1;
	if ($.trim(enname) == "" || $.trim(zhname) == "")
	{
		window.alert("请输入部门的英文和中文名！");
		return false;
	}
	if (enname.length > 30)
	{
		window.alert("部门的英文名不超过 30 个字符！");
		return false;
	}
	if (zhname.length > 15)
	{
		window.alert("部门的中文名不超过 15 个字！");
		return false;
	}
	if (belongto)
	{
		window.alert("请选择该职位隶属的部门！");
		return false;
	}
	return true;
}

// 初始化表单提交
function initFormSubmit()
{
	$("#submit_button").click(function(){
		if (!checkForm()) { return false; }
		var url = "set_position.php";
		var pars = {
			page_status: 2,
			enname: $("#add_block_enname").val(),
			zhname: $("#add_block_zhname").val(),
			belongto: $("#add_block_belongto_section").val()
		};
		$.post(url, pars, function(xmlobj){
			var text = $(xmlobj).find("msg").text();
			if (/Success/.test(text))
			{
				initDataTable(1);
				$("#opration_panel input").css("visibility", "visible");
				$.modal.close();
				$("#add_block_form").resetForm();
				$.get("make_oa_datas_js.php?status=2");
			}
			else
			{
				window.alert("提交失败！");
			}
		});
	});
}
